import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'couponPoolDefinitionM',

  state: {
    treeData: [],
    tableData: [],
    nameData: [],
    treeDatas: [],
    baselinePagination: {},
    currentUser: {
      roleModels: [],
      resources: [],
    },
  },

  effects: {
    //获取用户信息
    *getUserInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.user.queryCurrent, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: {
            currentUser: response.data,
          },
        });
        callback(response.data);
      }
    },

    //查询表格数据
    *saerchAll({ payload, callback }, { call, put }) {
      const response = yield call(api.definition.saerchAll, payload);

      if (response) {
        yield put({
          type: 'save',
          payload: {
            tableData: response.data,
            baselinePagination: {
              current: response.data.pageInfo.currentPage == null ? null : response.data.pageInfo.currentPage,
              pageSize: response.data.pageInfo.numInPage == null ? null : response.data.pageInfo.numInPage,
              total: response.data.pageInfo.total == null ? null : response.data.pageInfo.total,
            },
          },
        });
        callback(response.data.pageInfo);
      } else {
        message.error('查询失败');
      }
    },
    //获取部门树形下拉
    *getTree({ payload }, { call, put }) {
      const response = yield call(api.definition.getTree, payload);

      if (response) {
        yield put({
          type: 'save',
          payload: {
            treeDatas: response.data,
          },
        });
      } else {
        message.error('查询失败');
      }
    },

    //模糊查询
    *saerchName({ payload }, { call, put }) {
      const response = yield call(api.definition.saerchAll, payload);

      if (response) {
        yield put({
          type: 'save',
          payload: {
            nameData: response.data,
          },
        });
      } else {
        message.error('查询失败');
      }
    },

    //修改
    *editData({ payload }, { call, put }) {
      const res = yield call(api.definition.editData, payload);
      console.log('editData', res);
      if (res.code == 0) {
        message.success('修改成功');
      } else {
        message.error(res.msg || '操作失败');
      }
      return res;
    },

    //新增
    *addData({ payload }, { call, put }) {
      const response = yield call(api.definition.addData, payload);

      if (response.msg == 'success') {
        message.success('添加成功');
      } else {
        message.error(response.msg || '操作失败');
      }
      return response;
    },

    //删除数据
    *deleteData({ payload }, { call }) {
      // const id = payload.id

      const res = yield call(api.definition.deleteData, payload);
      console.log('deleteData', res);
      return res;
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
